package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.domain.SysMenu;
import com.itheima.youdianda.domain.SysRoleMenu;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.SysRole;
import com.itheima.youdianda.dto.RowDto;
import com.itheima.youdianda.dto.SysMenuVo;
import com.itheima.youdianda.dto.SysRoleDto;
import com.itheima.youdianda.service.SysMenuService;
import com.itheima.youdianda.service.SysRoleMenuService;
import com.itheima.youdianda.service.SysRoleService;
import com.itheima.youdianda.utils.DiGuiUtil;
import com.itheima.youdianda.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Pan Rundong
 * @Date 2023-02-13 14:35
 * @Desc 角色管理Controller
 */
@RestController
@Slf4j
@RequestMapping("/sys_role")
@Api(tags = "角色管理接口")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 角色分页查询
     *
     * @param page
     * @param limit
     * @param name
     * @return com.itheima.youdianda.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     * @Author: Pan Rundong
     * @Date: 2023-02-13 14:40
     * @Greet2u: （O｀ 3′O）~~♡
     */
    @GetMapping("/list")
    @ApiOperation("角色分页查询")
    public R<PageDto> page(@RequestHeader("x-token") String token, int page, int limit, String name) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            PageDto<SysRoleDto> pageDto = sysRoleService.getPage(page, limit, name);
            return R.success(pageDto);
        }
        return R.error("获取角色信息失败");
    }

    /**
     * 修改角色
     * @Author: Pan Rundong
     * @Date: 2023-02-13 15:08
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return com.itheima.youdianda.common.R<java.lang.String>
     */
    @PostMapping("/update")
    @ApiOperation("修改角色")
    public R<String> update(@RequestHeader("x-token") String token,@RequestBody SysRoleDto sysRoleDto) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            sysRoleService.updateByMenu(sysRoleDto);
            return R.success("角色信息修改成功");
        }
        return R.error("角色信息修改失败");
    }

    /**
     * 通过Id获取角色信息
     * @Author: Pan Rundong
     * @Date: 2023-02-13 15:15
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @param id
     * @return com.itheima.youdianda.common.R<com.itheima.youdianda.domain.SysRole>
     */

    @GetMapping("rowInfo")
    public R<RowDto> rowinfo(String id){
        SysRole byId = sysRoleService.getById(id);
        RowDto rowDto = new RowDto();
        BeanUtils.copyProperties(byId,rowDto);
        //根据roleid查出对应的menuid
        LambdaQueryWrapper<SysRoleMenu>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(byId.getId()!=null,SysRoleMenu::getRoleId,byId.getId());
        queryWrapper.select(SysRoleMenu::getMenuId);
        List<Integer> integers = sysRoleMenuService.listObjs(queryWrapper, (item) -> {
            return Integer.parseInt(item.toString());
        });
        List<SysMenu> sysMenus = sysMenuService.listByIds(integers);

        List<SysMenuVo> collect = sysMenus.stream().map((item) -> {
            SysMenuVo sysMenuVo = new SysMenuVo();
            BeanUtils.copyProperties(item, sysMenuVo);
            return sysMenuVo;
        }).collect(Collectors.toList());
        Integer min = Collections.min(integers);

        DiGuiUtil diGuiUtil = new DiGuiUtil();
        List<SysMenuVo> chind = diGuiUtil.getChind(min, collect);
        rowDto.setAuthList(chind);
        return R.success(rowDto);
    }


    /**
     * 获取全部角色信息
     * @Author: Pan Rundong
     * @Date: 2023-02-13 16:57
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @return com.itheima.youdianda.common.R<java.util.List < com.itheima.youdianda.domain.SysRole>>
     */
    @GetMapping("/allData")
    @ApiOperation("获取全部角色信息")
    public R<List<SysRole>> allDate(@RequestHeader("x-token") String token) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            List<SysRole> list = sysRoleService.list();
            return R.success(list);
        }
        return R.error("获取角色信息失败");
    }

    /**
     * 添加角色
     * @Author: Pan Rundong
     * @Date: 2023-02-13 20:02
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @param sysRoleDto
     * @return com.itheima.youdianda.common.R<java.lang.String>
     */
    @PostMapping("/add")
    @ApiOperation("添加角色")
    public R<String> save(@RequestHeader("x-token") String token,@RequestBody SysRoleDto sysRoleDto) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            sysRoleService.saveByMenus(sysRoleDto);
            return R.success("添加角色成功");
        }
        return R.error("添加角色失败");
    }

    /**
     * 删除角色
     * @Author: Pan Rundong
     * @Date: 2023-02-13 20:31
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @param id
     * @return com.itheima.youdianda.common.R<java.lang.String>
     */
    @ApiOperation("删除角色")
    @GetMapping("/del")
    public R<String> delete(@RequestHeader("x-token") String token, String id) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            sysRoleService.deleteByMenus(id);
            return R.success("删除角色成功");
        }
        return R.error("删除角色失败");
    }
}
